package com.shop.admin.service.admin;

import com.shop.admin.dao.admin.AdminDao;
import com.shop.admin.dao.admin.AdminSQLDao;
import com.shop.admin.property.BusinessProps;
import com.shop.admin.reqeust.admin.AdminEditReq;
import com.shop.admin.reqeust.admin.AdminPageReq;
import com.shop.admin.reqeust.admin.AdminReq;
import com.shop.admin.response.AdminRetEnum;
import com.shop.common.enums.admin.AdminLockEnum;
import com.shop.domain.admin.Admin;
import com.shop.util.BeanCopyUtils;
import com.shop.util.IdSignUtil;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Transactional
@Service
@Slf4j
public class AdminService {

    @Autowired
    private BusinessProps businessProps;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private AdminSQLDao adminSQLDao;

    public Admin findById(Long adminId) {
        // 返回用户信息
        return adminDao.findSingle(adminId);
    }

    public BaseResponse login(String mobile, String pwd) {
        Admin admin = adminDao.findAdminByMobileOrName(mobile);


        // 返回用户信息
        return new BaseResponse(admin);
    }

    public BaseResponse loginOut(String accessToken) {

        return new BaseResponse();
    }

    public BaseResponse modifyPwd(Long adminId, String oldPwd, String newPwd) {
        Admin admin = adminDao.findSingle(adminId);
        if (null == admin) {
            return new BaseResponse(AdminRetEnum.ADMIN_NOT_EXISTS);
        }



        return new BaseResponse();
    }

    public BaseResponse modifyInfo(Long adminId, String mobile, String realityName, String email) {
        Admin admin = adminDao.findSingle(adminId);
        if (null == admin) {
            return new BaseResponse(AdminRetEnum.ADMIN_NOT_EXISTS);
        }

        admin.setEmail(email);
        admin.setMobile(mobile);
        admin.setRealityName(realityName);
        adminDao.save(admin);

        return new BaseResponse(admin);
    }

    public BaseResponse page(AdminPageReq adminPageReq) {
        return new BaseResponse(adminSQLDao.page(adminPageReq));
    }

    public BaseResponse save(AdminReq adminReq) {
        if(null != adminDao.findAdminByMobileOrName(adminReq.getName())) {
            return new BaseResponse(AdminRetEnum.ADMIN_NAME_IS_EXISTS);
        }

        Admin admin = new Admin();

        try {
            BeanCopyUtils.copyProperties(admin, adminReq);
        } catch (Exception e) {
            log.error("保存管理员时赋值异常！", e);
            return BaseResponse.SYSTEM_EXCEPTION;
        }



        admin.setLockType(AdminLockEnum.NORMAL.getCode());

        adminDao.save(admin);

        return new BaseResponse(admin);
    }
    public BaseResponse edit(AdminEditReq adminEditReq) {
        Long adminId = IdSignUtil.checkSign(adminEditReq.getSign());
        if(null == adminId) {
            return BaseResponse.ILLEGAL_REQUEST;
        }

        Admin admin = adminDao.findSingle(adminId);
        if(null == admin) {
            return new BaseResponse(AdminRetEnum.ADMIN_NOT_EXISTS);
        }
        if(StringUtils.isNotBlank(adminEditReq.getName())) {  // 修改名称
            Admin updateNameAdmin = adminDao.findAdminByMobileOrName(adminEditReq.getName());
            if(null != updateNameAdmin) {   // 用户名已存在
                if(admin.getId().intValue() != updateNameAdmin.getId().intValue()) {  // 判断是否同一个账号
                    return new BaseResponse(AdminRetEnum.ADMIN_NAME_IS_EXISTS);
                }
            }
        }

        try {
            BeanCopyUtils.copyProperties(admin, adminEditReq);
        } catch (Exception e) {
            log.error("保存管理员时赋值异常！", e);
            return BaseResponse.SYSTEM_EXCEPTION;
        }

        adminDao.save(admin);
        return new BaseResponse(admin);
    }

    public BaseResponse del(Long id) {
        Admin admin = adminDao.findSingle(id);
        if (null == admin) {
            return new BaseResponse(AdminRetEnum.ADMIN_NOT_EXISTS);
        }

        adminDao.deleteById(id);
        return new BaseResponse();
    }

    public BaseResponse lock(Long id) {
        Admin admin = adminDao.findSingle(id);
        if (null == admin) {
            return new BaseResponse(AdminRetEnum.ADMIN_NOT_EXISTS);
        }

        int rows = adminDao.lock(id, AdminLockEnum.ADMIN_LOCK.getCode());
        if(rows <= 0) {
            return BaseResponse.FAIL;
        }

        return new BaseResponse();
    }

}
